home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / File / CheckTree.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  4.0 KB  |  126 lines

  1. package File::CheckTree;
  2. require 5.000;
  3. require Exporter;
  4.  
  5. =head1 NAME
  6.  
  7. validate - run many filetest checks on a tree
  8.  
  9. =head1 SYNOPSIS
  10.  
  11.     use File::CheckTree;
  12.  
  13.     $warnings += validate( q{
  14.     /vmunix                 -e || die
  15.     /boot                   -e || die
  16.     /bin                    cd
  17.         csh                 -ex
  18.         csh                 !-ug
  19.         sh                  -ex
  20.         sh                  !-ug
  21.     /usr                    -d || warn "What happened to $file?\n"
  22.     });
  23.  
  24. =head1 DESCRIPTION
  25.  
  26. The validate() routine takes a single multiline string consisting of
  27. lines containing a filename plus a file test to try on it.  (The
  28. file test may also be a "cd", causing subsequent relative filenames
  29. to be interpreted relative to that directory.)  After the file test
  30. you may put C<|| die> to make it a fatal error if the file test fails.
  31. The default is C<|| warn>.  The file test may optionally have a "!' prepended
  32. to test for the opposite condition.  If you do a cd and then list some
  33. relative filenames, you may want to indent them slightly for readability.
  34. If you supply your own die() or warn() message, you can use $file to
  35. interpolate the filename.
  36.  
  37. Filetests may be bunched:  "-rwx" tests for all of C<-r>, C<-w>, and C<-x>.
  38. Only the first failed test of the bunch will produce a warning.
  39.  
  40. The routine returns the number of warnings issued.
  41.  
  42. =cut
  43.  
  44. @ISA = qw(Exporter);
  45. @EXPORT = qw(validate);
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. sub validate {
  53.     local($file,$test,$warnings,$oldwarnings);
  54.     foreach $check (split(/\n/,$_[0])) {
  55.     next if $check =~ /^#/;
  56.     next if $check =~ /^$/;
  57.     ($file,$test) = split(' ',$check,2);
  58.     if ($test =~ s/^(!?-)(\w{2,}\b)/$1Z/) {
  59.         $testlist = $2;
  60.         @testlist = split(//,$testlist);
  61.     }
  62.     else {
  63.         @testlist = ('Z');
  64.     }
  65.     $oldwarnings = $warnings;
  66.     foreach $one (@testlist) {
  67.         $this = $test;
  68.         $this =~ s/(-\w\b)/$1 \$file/g;
  69.         $this =~ s/-Z/-$one/;
  70.         $this .= ' || warn' unless $this =~ /\|\|/;
  71.         $this =~ s/^(.*\S)\s*\|\|\s*(die|warn)$/$1 || valmess('$2','$1')/;
  72.         $this =~ s/\bcd\b/chdir (\$cwd = \$file)/g;
  73.         eval $this;
  74.         last if $warnings > $oldwarnings;
  75.     }
  76.     }
  77.     $warnings;
  78. }
  79.  
  80. sub valmess {
  81.     local($disposition,$this) = @_;
  82.     $file = $cwd . '/' . $file unless $file =~ m|^/|;
  83.     if ($this =~ /^(!?)-(\w)\s+\$file\s*$/) {
  84.     $neg = $1;
  85.     $tmp = $2;
  86.     $tmp eq 'r' && ($mess = "$file is not readable by uid $>.");
  87.     $tmp eq 'w' && ($mess = "$file is not writable by uid $>.");
  88.     $tmp eq 'x' && ($mess = "$file is not executable by uid $>.");
  89.     $tmp eq 'o' && ($mess = "$file is not owned by uid $>.");
  90.     $tmp eq 'R' && ($mess = "$file is not readable by you.");
  91.     $tmp eq 'W' && ($mess = "$file is not writable by you.");
  92.     $tmp eq 'X' && ($mess = "$file is not executable by you.");
  93.     $tmp eq 'O' && ($mess = "$file is not owned by you.");
  94.     $tmp eq 'e' && ($mess = "$file does not exist.");
  95.     $tmp eq 'z' && ($mess = "$file does not have zero size.");
  96.     $tmp eq 's' && ($mess = "$file does not have non-zero size.");
  97.     $tmp eq 'f' && ($mess = "$file is not a plain file.");
  98.     $tmp eq 'd' && ($mess = "$file is not a directory.");
  99.     $tmp eq 'l' && ($mess = "$file is not a symbolic link.");
  100.     $tmp eq 'p' && ($mess = "$file is not a named pipe (FIFO).");
  101.     $tmp eq 'S' && ($mess = "$file is not a socket.");
  102.     $tmp eq 'b' && ($mess = "$file is not a block special file.");
  103.     $tmp eq 'c' && ($mess = "$file is not a character special file.");
  104.     $tmp eq 'u' && ($mess = "$file does not have the setuid bit set.");
  105.     $tmp eq 'g' && ($mess = "$file does not have the setgid bit set.");
  106.     $tmp eq 'k' && ($mess = "$file does not have the sticky bit set.");
  107.     $tmp eq 'T' && ($mess = "$file is not a text file.");
  108.     $tmp eq 'B' && ($mess = "$file is not a binary file.");
  109.     if ($neg eq '!') {
  110.         $mess =~ s/ is not / should not be / ||
  111.         $mess =~ s/ does not / should not / ||
  112.         $mess =~ s/ not / /;
  113.     }
  114.     print STDERR $mess,"\n";
  115.     }
  116.     else {
  117.     $this =~ s/\$file/'$file'/g;
  118.     print STDERR "Can't do $this.\n";
  119.     }
  120.     if ($disposition eq 'die') { exit 1; }
  121.     ++$warnings;
  122. }
  123.  
  124. 1;
  125.  
  126.